package com.koron.plan.web.maintainplan.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.plan.web.common.CommonUtils;
import com.koron.plan.web.maintainplan.bean.entity.MaintainPlanDispatchBean;
import com.koron.plan.web.maintainplan.mapper.MaintainPlanDispatchMapper;
import com.koron.plan.web.maintainplan.service.MaintainPlanDispatchService;
import org.apache.commons.lang.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author twcao
 * @title: MaintainPlanDispatchServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 派工信息service实现
 * @date 2021/9/29:07
 */
@Service
public class MaintainPlanDispatchServiceImpl implements MaintainPlanDispatchService {

    /**
     * 保存或更新
     * @param factory 会话
     * @param bean    待保存的实体
     * @return 是否保存成功
     */
    @Override
    public boolean saveOrUpdate(SessionFactory factory, MaintainPlanDispatchBean bean) {
        if(Objects.isNull(bean)) {
            return true;
        }
        MaintainPlanDispatchMapper dispatchMapper = factory.getMapper(MaintainPlanDispatchMapper.class);
        if(StringUtils.isBlank(bean.getId())) {
            CommonUtils.update(bean);
            bean.setId(CodeTools.getCode32());
        }
        return dispatchMapper.saveOrUpdate(bean);
    }

    /**
     * 保存或更新
     *
     * @param factory 会话
     * @param planId  计划id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteByPlanId(SessionFactory factory, String planId) {
        if(StringUtils.isBlank(planId)) {
            return true;
        }
        MaintainPlanDispatchMapper dispatchMapper = factory.getMapper(MaintainPlanDispatchMapper.class);
        return dispatchMapper.deleteByPlanId(planId) > 0;
    }
}
